package com.jyw.business.ast.domain.dto.web;

import com.alibaba.excel.annotation.ExcelProperty;
import com.jyw.business.ast.domain.AstConsumable;
import com.jyw.common.constant.SysDictTypeConstant;
import com.jyw.common.utils.DictUtils;
import com.jyw.common.utils.bean.BeanUtils;
import com.jyw.common.utils.spring.SpringUtils;
import com.jyw.system.service.ISysDictDataService;
import com.jyw.system.utils.DeptUtils;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.ObjectUtils;

import java.math.BigDecimal;

@Data
@ApiModel("耗材导入请求实体")
@Slf4j
public class AstConsumableImportDto {

    @ApiModelProperty("耗材名称")
    @ExcelProperty(index = 0)
    public String name;

    @ApiModelProperty("规格型号")
    @ExcelProperty(index = 1)
    public String model;

    @ApiModelProperty("供应商")
    @ExcelProperty(index = 2)
    public String supplierName;

    @ApiModelProperty("分类")
    @ExcelProperty(index = 3)
    public String type;

    @ApiModelProperty("使用单位")
    @ExcelProperty(index = 4)
    public String unit;

    @ApiModelProperty("安全库存")
    @ExcelProperty(index = 5)
    public Integer safeReserve;

    @ApiModelProperty("单价")
    @ExcelProperty(index = 6)
    public String price;

    @ApiModelProperty("备注")
    @ExcelProperty(index = 7)
    public String remark;

    public AstConsumable buildConsumable(){
        AstConsumable consumable = new AstConsumable();
        BeanUtils.copyProperties(this,consumable);
        Long supplierId = DeptUtils.nameToId(getSupplierName());
        consumable.setSupplierId(supplierId);
        consumable.setType(SpringUtils.getBean(ISysDictDataService.class).getDictValueSave(SysDictTypeConstant.AST_CONSUMABLE_TYPE,getType()));
        consumable.setUnit(SpringUtils.getBean(ISysDictDataService.class).getDictValueSave(SysDictTypeConstant.AST_CONSUMABLE_UNIT,getUnit()));
        consumable.setRecordState(1);
        consumable.setRealReserve(0);
        consumable.setFrostReserve(0);
        if(ObjectUtils.isEmpty(getPrice())){
            try {
                consumable.setPrice(new BigDecimal(getPrice()));
            }catch (Exception e){
                log.error("导入耗材单价转换报错:{}",getPrice(),e);
            }
        }
        return consumable;
    }
}
